﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_Modes;
using Zor_IDAL;
using Zor_Tools;
using System.Data;
using System.Threading;

namespace Zor_DAL
{
    /// <summary>
    /// 部门管理
    /// </summary>
    public class Sys_Department_DAL<T> where T : class
    {
        /// <summary>
        /// 获取部门管理列表
        /// create liuqi by 2017.3.29
        /// </summary>
        /// <returns></returns>
        public static List<T> GeDepartmentList()
        {
            var sql = "select *,Dt_Name as name from  Sys_Department where Dt_IsDelete=0 order by dt_id";
            return DapperQuery<T>.DoQueryList(sql, null);
        }

        /// <summary>
        /// 获取部门不为4的数据
        /// create liuqi by 2017.3.29
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns> 
        public static List<T> SelectDtparentid()
        {
            var sql = @"select * from  Sys_Department where Dt_IsDelete=0 and  dt_level<>4 order by dt_level ";
            List<T> rsDepartmentList = DapperQuery<T>.DoQueryList(sql, null);
            return rsDepartmentList;
        }

        /// <summary>
        /// 获取部门
        /// create liuqi by 2017.3.29
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回对象</returns>
        public static List<T> SelectDepartmentID(Sys_Department depment)
        {
            var sql = @"select * from  Sys_Department 
                        where Dt_IsDelete=0 and Dt_Id = @Dt_Id";
            List<T> rsDepartmentList = DapperQuery<T>.DoQueryList(sql, new { Dt_Id = depment.Dt_Id });
            return rsDepartmentList;
        }

        /// <summary>
        /// 新增部门
        /// create liuqi by 2017.3.29
        /// </summary>
        /// <param name="department">部门</param>
        /// <returns></returns>
        public static BaseAnswer InsertDepartment(Sys_Department department)
        {
            var sql = @"if not exists(select Dt_Name from Sys_Department where Dt_Name =@Dt_Name and Dt_isDelete=0 ) 
                        insert into Sys_Department values 
                        (@Dt_Name,@Dt_ParentId,@Dt_AmdCode,@Dt_AmdFullName,@Dt_BD_Longitude,@Dt_BD_Latitude,@Dt_Sortnum,@Dt_Level,0)";
            BaseAnswer bar = DapperInsert<T>.DoInsert(sql, new
            {
                Dt_Name = department.Dt_Name,
                Dt_ParentId = department.Dt_ParentId,
                Dt_AmdCode = department.Dt_AmdCode,
                Dt_AmdFullName = department.Dt_AmdFullName,
                Dt_BD_Longitude = department.Dt_BD_Longitude,
                Dt_BD_Latitude = department.Dt_BD_Latitude,
                Dt_Sortnum = department.Dt_Sortnum,
                Dt_Level = department.Dt_Level
            });
            return bar;
        }

        /// <summary>
        /// 修改
        /// create liuqi by 2017.3.29
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        public static BaseAnswer AlterDepartment(Sys_Department department, string oldName)
        {
            var sql = "";
            if (department.Dt_Name != oldName)//旧名称和新名称不一致,判断是否重名
            {
                sql = @"if not exists(select Dt_Name from Sys_Department where Dt_Name =@Dt_Name and Dt_isDelete=0  )";
            }
            sql += @"update [Sys_Department] set Dt_Name=@Dt_Name,Dt_ParentId=@Dt_ParentId,Dt_Level=@Dt_Level, 
                   Dt_AmdCode=@Dt_AmdCode,Dt_AmdFullName=@Dt_AmdFullName,Dt_BD_Longitude=@Dt_BD_Longitude,
                   Dt_BD_Latitude=@Dt_BD_Latitude,Dt_Sortnum=@Dt_Sortnum 
                   where Dt_Id = @Dt_Id";
            BaseAnswer bar = DapperEdit<T>.DoEdit(sql,
                new
                {
                    Dt_Name = department.Dt_Name,
                    Dt_ParentId = department.Dt_ParentId,
                    Dt_Level = department.Dt_Level,
                    Dt_AmdCode = department.Dt_AmdCode,
                    Dt_AmdFullName = department.Dt_AmdFullName,
                    Dt_BD_Longitude = department.Dt_BD_Longitude,
                    Dt_BD_Latitude = department.Dt_BD_Latitude,
                    Dt_Sortnum = department.Dt_Sortnum,
                    Dt_Id = department.Dt_Id
                });
            return bar;
        }

        /// <summary>
        /// 删除部门
        /// create liuqi by 2017.3.29
        /// </summary>
        /// <param name="id">删除的Id</param>
        /// <returns></returns>
        public static BaseAnswer DeleteDepartment(string id)
        {
            var sql = @"update Sys_Department set Dt_IsDelete=1 where Dt_Id=@Dt_id";
            BaseAnswer bar = DapperDelete<T>.DoDelete(sql, new { Dt_id = id });
            return bar;
        }

        /// <summary>
        /// 查询要删除的部门是否拥有下级部门
        /// create liuqi by 2017.3.29
        /// </summary>
        /// <param name="id">查询删除的Id</param>
        /// <returns>查询的数据</returns>
        public static List<T> SelectDepartment(string id)
        {
            var sql = @"select * from  dbo.Sys_Department a 
                        join dbo.Sys_Department  b 
                        on a.Dt_id=b.Dt_ParentId 
                        where  b.Dt_IsDelete=0 and b.Dt_Parentid=@Dt_id";
            List<T> rsDepartmentList = DapperQuery<T>.DoQueryList(sql, new { Dt_id = id });
            return rsDepartmentList;
        }

    }
}
